package controllers.manage.merchant;

import controllers.BaseController;
import helper.AliyunOssUploader;
import helper.GlobalConfig;
import jodd.util.StringUtil;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import models.weixin.WeixinData;
import play.Logger;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商户管理
 * Created by hanbin on 17/4/6.
 */
@With(BaseController.class)
public class MerChantCtrl extends Controller {

    /**
     * 跳转到商户管理页面
     */
    public static void index(Integer pageNumber, String name){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String , Object> searchMap = new HashMap<>();
        if(StringUtil.isNotBlank(name)) {
            searchMap.put("name", "%"+name+"%");
        }

        flash.put("menuId", "2");

        JPAExtPaginator<Merchant> resultPage = Merchant.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber, name);
    }

    /**
     * 跳转到新增商户页面
     */
    public static void add(){
        List<WeixinData> weixinDatas = WeixinData.findAllWeixinData();
        render(weixinDatas);
    }

    /**
     * 跳转到修改商户页面
     */
    public static void modify(long id, Integer pageNumber){
        List<WeixinData> weixinDatas = WeixinData.findAllWeixinData();
        Merchant merchant = Merchant.findById(id);
        render(merchant, pageNumber, weixinDatas);
    }


    /**
     * 创建商户
     */
    public static void create(Merchant merchant , File imagePath){
        try {
            if(imagePath != null) {
                BufferedImage sourceImg = ImageIO.read(new FileInputStream(imagePath));
                Integer width = sourceImg.getWidth();
                Integer height = sourceImg.getHeight();
                if(((double)width/(double)height) != ((double)4/(double)3)){
                    merchant.deleted = DeletedStatus.UN_DELETED;
                    merchant.save();
                    flash.put("msg" , "图片比例不是4:3");
                    modify(merchant.id, 1);
                }
                merchant.imagePath = AliyunOssUploader.uploadPublicFile(imagePath , 300);
            }
            merchant.deleted = DeletedStatus.UN_DELETED;
            merchant.save();
            flash.put("msg" , "商户创建成功");
            redirect("/BS0007/admin/merchant/index/1");
        } catch (Exception e) {
            Logger.warn(e, "LOG01750: merchant create failed!");
        }
    }

    /**
     * 修改商户
     */
    public static void update(Long merchantId, Merchant merchant, Integer pageNumber , File imagePath){
        try {
            if(imagePath != null) {
                BufferedImage sourceImg = ImageIO.read(new FileInputStream(imagePath));
                Integer width = sourceImg.getWidth();
                Integer height = sourceImg.getHeight();
                if(((double)width/(double)height) != ((double)4/(double)3)){
                    Merchant.update(merchantId , merchant);
                    flash.put("msg" , "图片比例不是4:3");
                    modify(merchantId, pageNumber);
                }
                merchant.imagePath = AliyunOssUploader.uploadPublicFile(imagePath , 300);
            }
            Merchant.update(merchantId , merchant);
            flash.put("msg" , "商户修改成功");
            redirect("/BS0007/admin/merchant/index/"+pageNumber);
        } catch (Exception e) {
            Logger.warn(e, "LOG01750: merchant update failed!");
        }
    }

    /**
     * 删除商户
     */
    public static void delete(long id , Integer pageNumber){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Merchant merchant = Merchant.findById(id);
        //判断merchant是否被使用
        if(Merchant.checkIsUsed(id)){
            flash.put("msg" , "场馆正在被使用,无法删除!");
            redirect("/BS0007/admin/merchant/index/"+pageNumber);
        }
        merchant.deleted=DeletedStatus.DELETED;
        merchant.save();
        flash.put("msg" , "场馆删除成功");
        redirect("/BS0007/admin/merchant/index/"+pageNumber);
    }

}
